<?php

namespace App\Http\Controllers\Admin;
header('Content-Type:text/html;charset=utf-8');


use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Route,URL,Auth;


class TzController extends Controller
{
	 public function index(request $request){

		 	if(Auth::guard('admin')->user()->role_id == 22){
				$map['tongzhi.type'] = '1';
			}else{
				$map = array();
			}

	 	   if($request->rows) {
	 		   $res = DB::table('tongzhi')
	 		   ->where($map)
	 	       ->join('admin_users', 'tongzhi.uid', '=', 'admin_users.id')
	 	       ->select('tongzhi.*','admin_users.name')
	 	       ->orderBy('tz_time','desc')
	 	       ->paginate($request->rows);

	 	   		 $list = $res->items();

	 	   		 foreach ($list as $k=>$v){
	                $read = array(
	                    'unit_id' => Auth::guard('admin')->user()->unit_id,
	                    'tz_id'=> $v->tz_id,
	                );
	                $readok = DB::table('readtz')->where($read)->first();
	                if (!$readok) {
	                    $list[$k]->read = '0';
	                }else{
	                    $list[$k]->read = '1';
	                }
	            }


	 	   		 $result['total'] = $res->total();
		         $result['rows'] = $list;
		         echo json_encode($result);
	 	   }else{
	 	   		 return view('admin.tz.index');
	 	   }
	 }

	 public function addtz(request $request){

	 	 if($request->isMethod('post')){
            $input = $request->all();
            $data = Input::except('_token');


            $data = array(
                'tz_title' => $input['tz_title'],
                'tz_cont' => $input['tz_cont'],
                'tz_time' => $input['tz_time'],
                'type' => $input['type'],
                'uid' => Auth::guard('admin')->user()->id,
                'created_at' => date('Y-m-d H:i:s',time()),

            );
			 foreach ($data as $k=>$v){
				 $data[$k] = htmlspecialchars($v);
			 }

            $abg = DB::table('tongzhi')->insert($data);
            if ($abg) {
                 return ['status'=>1,'msg'=>'添加成功'];
            }else{
                 return ['status'=>0,'msg'=>'添加失败'];
            }
           
        }


	 	return view('admin.tz.addtz');
	 }


	 public function tzedit($id,request $request){

	 	   if($request->isMethod('post')){

            $input = $request->all();
            $data = Input::except('_token');
                 
            $datas = array(
                'tz_title' => $input['tz_title'],
                'tz_cont' => $input['tz_cont'],
                'tz_time' => $input['tz_time'],
                'type' => $input['type'],
                'uid' => Auth::guard('admin')->user()->id,
                'update_at' => date('Y-m-d H:i:s',time()),
            );
			   foreach ($datas as $k=>$v){
				   $datas[$k] = htmlspecialchars($v);
			   }

	            $upjq = DB::table('tongzhi')->where('tz_id',$id)->update($datas);

	            if ($upjq) {
	                 return ['status'=>1,'msg'=>'修改成功'];
	            }else{
	                 return ['status'=>0,'msg'=>'修改失败'];
	            }

	        }
	 	 

	        //判断企业是否已读
	        if (Auth::guard('admin')->user()->role_id == 22) {
	             $maps = array(
	                'unit_id' => Auth::guard('admin')->user()->unit_id,
	                'tz_id' => $id,
	              );
	             $findread = db::table('readtz')->where($maps)->first();
	             if (!$findread) {
	                $addread =  db::table('readtz')->insert($maps);
	             }
	        }




          $map['tz_id'] = $id;
	 	  $re = DB::table('tongzhi')->where($map)->first();
	 	 return view('admin.tz.tzedit', ['id'=>$id,'re'=>$re]);
	 }

	   //删除
    public function del($id,Request $request){

        $re = db::table('tongzhi')->where('tz_id',$id)->delete();
        if($re){
            return ['status'=>1,'msg'=>'删除成功'];
        }else{
            return ['status'=>0,'msg'=>'删除失败'];
        }
    }




    

}